package com.dj.mingkong.admin.biz.controller.system;

import com.dj.mingkong.admin.biz.controller.system.vo.request.UserRoleUpdateVo;
import com.dj.mingkong.admin.biz.controller.system.vo.response.UserRoleVo;
import com.dj.mingkong.admin.biz.service.system.UserRoleService;
import com.dj.mingkong.common.annotation.HasRoles;
import com.dj.mingkong.common.enums.RoleEnum;
import com.dj.mingkong.common.infrastructure.base.dto.rest.RestResponse;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 用户角色管理
 *
 * @author zenglonghui
 * @date: 2022/8/16 9:24
 */
@RestController
@RequestMapping("/user/role")
@HasRoles(RoleEnum.ADMIN)
public class UserRoleController {
    @Resource
    private UserRoleService userRoleService;

    /**
     * 获取用户角色信息
     *
     * @param userId 用户Id
     * @return UserRoleVo
     */
    @GetMapping("/listByUserId/{userId}")
    public RestResponse<UserRoleVo> listByUserId(@PathVariable("userId") Long userId) {
        return RestResponse.success(userRoleService.listByUserId(userId));
    }

    /**
     * 修改数据
     *
     * @param updateVo 实例对象
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException userRoleVo. getId()为空的时候会抛出运行时异常
     */
    @PostMapping("/updateByUserId")
    public RestResponse<Boolean> updateByUserId(@RequestBody @Validated UserRoleUpdateVo updateVo) {
        userRoleService.updateByUserId(updateVo);
        return RestResponse.buildSuccess();
    }
}
